package org.testis.webautomationmodule.ui;

import org.testis.logging.AtofLogLevel;
import org.testis.webautomationmodule.services.WebAutomationProviderImpl;


/**
 *Defines base decorator of ui objects
 * @author nurhat
 */
public abstract class WebAutomationDecorator
{

    protected WebAutomationDecorator m_nextUIObject;
    /**
     * Defines WebAutomation implementation to be used
     */
    protected WebAutomationProviderImpl m_webAutomationImpl;
    /**
     * Unique message id given to test step data object to
     */
    private String m_messageId;
    private String m_abstractId;


    /**
     * Extended decorator method, special for each object
     * @return
     */
    protected boolean setBehavior()
    {
        return true;
    }


    /**
     * Extended decorator method, special for each object
     * @return
     */
    protected boolean verifyBehavior()
    {
        return true;
    }


    /**
     * Extended decorator method, special for each object
     * @return
     */
    protected boolean getBehavior()
    {
        return true;
    }


    public WebAutomationDecorator getNextUIObjectDecorator()
    {
        return m_nextUIObject;
    }


    public WebAutomationProviderImpl getWebAutomationImpl()
    {

        return m_webAutomationImpl;
    }


    /**
     * Operates current decorators special method(()), and delegates remaining job
     * to the related object(m_nextUIObject).
     * @return
     */
    public boolean operate()
    {
        if (getWebAutomationImpl() == null) {
            putLog(" Web automation implementation must be set for the object",AtofLogLevel.ERROR);
            return false;
        }
        else if (setBehavior()) {
            if (m_nextUIObject != null) {
                return m_nextUIObject.operate();
            }
            return true;
        }
        return false;
    }


    /**
     * Operates current decorators special method(()), and delegates remaining job
     * to the related object(m_nextUIObject).
     * @return
     */
    public boolean verify()
    {
        boolean result;
        if (getWebAutomationImpl() == null) {
            putLog(" Web automation implementation must be set for the object",AtofLogLevel.ERROR);
            return false;
        }
        result = verifyBehavior();

        if (m_nextUIObject != null) {
            result = result & m_nextUIObject.verify();
        }
        return result;
    }


    /**
     * Operates current decorators special method(()), and delegates remaining job
     * to the related object(m_nextUIObject).
     * @return
     */
    public boolean get()
    {
        boolean result;
        if (getWebAutomationImpl() == null) {
            putLog(" Web automation implementation must be set for the object",AtofLogLevel.ERROR);
            return false;
        }
        result = getBehavior();

        if (m_nextUIObject != null) {
            result = result & m_nextUIObject.get();
        }
        return result;
    }


    /**
     * For logging outputs of ui objects' actions
     * @param msg
     */
    public void putLog(String msg)
    {
        String className = super.getClass().getSimpleName();
        org.testis.logging.LogSenderService.putMessageOnQueue(className,
                                                                    AtofLogLevel.INFO,
                                                                    msg,
                                                                    getMessageId());
    }


    /**
     * For logging outputs of ui objects' actions
     * @param msg
     */
    public void putLog(String msg,
                       AtofLogLevel logLevel)
    {
        String className = super.getClass().getSimpleName();
        org.testis.logging.LogSenderService.putMessageOnQueue(className,
                                                                    logLevel,
                                                                    msg,
                                                                    getMessageId());
    }


    /**
     * 
     * @param val
     */
    public void setNextUIObjectDecorator(WebAutomationDecorator val)
    {
        this.m_nextUIObject = val;

    }


    /**
     * 
     * @param implementation
     */
    public void setWebAutomationImpl(WebAutomationProviderImpl implementation)
    {
        this.m_webAutomationImpl = implementation;
    }


    public String getMessageId()
    {
        return m_messageId;
    }


    public void setMessageId(String m_messageId)
    {
        this.m_messageId = m_messageId;
    }


    public String getAbstractId()
    {
        return m_abstractId;
    }


    public void setAbstractId(String m_abstractId)
    {
        this.m_abstractId = m_abstractId;
    }
}

